<?php

class MemberController extends Controller {

	protected $db;

	function __construct($db) {
	
		$this->db = $db;
		
		$viewItem = 'login';
		if (isset($_SESSION['username']) && $_GET['view'] != 'logout' && $_GET['view'] != 'profile') {
			$this->view = new ConfirmationMessageView("<p>Welcome to our e-shop</p>");
			$this->view->setTitle("Home page"); 
		}
		else {
			if (isset($_GET['view'])) {
				
					$viewItem = $_GET['view'];
			}
			if ($viewItem == 'register') {
					if (isset($_POST['action'])) {
						if ($_POST['action'] == "save") {
							
							if ($_POST['pass'] != $_POST['repeatpass']) {
								$this->view = new RegisterView();
								$this->view->setTitle("Registration");
								$this->view->setErrorMessage("<p style='color:red;'>Passwords don't match!</p>");
								return;
							}
							
							$member = new Member(-1, $_POST['fname'], $_POST['lname'], $_POST['ctype'], $_POST['cnumber'], $_POST['address'], $_POST['phone'], false);
							if (!$this->addMember($member)) {
								$this->view = new RegisterView();
								$this->view->setTitle("Registration");
								$this->view->setErrorMessage("<p style='color:red;'>Username already exists</p>");
								return;
							}
							$_SESSION['username'] = $_POST['uname'];
							$_SESSION['type'] = 0;
							$this->view = new ConfirmationMessageView("<p>User registration successful</p>");
							$this->view->setTitle("Welcome to our e-shop"); 
							return;
						}
					}
					$this->view = new RegisterView();
					$this->view->setTitle("Registration");
				}
			else if ($viewItem == 'login') {
					if (isset($_POST['action'])) {
						if ($_POST['action'] == "save") {
							if ($this->validateMember($_POST['username'], md5($_POST['password']))) {
								$this->view = new ConfirmationMessageView("<p>Login successful! Press <a href='?index.php'>here</a> to get to the home page</p>");
							}
							else {
								$this->view = new LoginView();
								$this->view->setError(true);
								$this->view->setTitle('Login');
							}
						}
						else {
							$this->view = new LoginView();
							$this->view->setError(true);
							$this->view->setTitle('Login');
						}
					}
					else {
						$this->view = new LoginView();
						$this->view->setTitle('Login');
					}
				}
			else if ($viewItem == 'logout') {
					session_destroy();
					header("Location: index.php");
					die;
				}
			else if ($viewItem == 'profile') {
					$this->view = new ProfileView();
					$user = $this->getMember($_SESSION['username']);
					$this->view->setUser($user);
					$sales = $this->getHistorySales($user->getId());
					$this->view->setSales($sales);
					$this->view->setTitle('Profile');
				}
		}
	}
	
	function __autoload() {
		include 'Models/Member.php';
	}
	
	function addMember($member) {
		return $this->db->addMember($member->getFirstName(), $member->getLastName(), $_POST['uname'], $_POST['pass'], $member->getCreditType(), $member->getCreditNumber(), $member->getAddress(), $member->getIsManager(), $member->getPhoneNumber());
	}
	
	function validateMember($username, $password) {
		$results = $this->db->validateMember($username, $password);
		$row = $results->fetch_row();
		if (!$row) {
			return false;
		}
		$user = new Member($row[0], $row[1], $row[2], $row[4], $row[5], $row[6], $row[7], $row[8]);
		$_SESSION['username'] = $row[3];
		$_SESSION['type'] = $row[8];
		
		return true;
	}
	
	function getHistorySales($id) {
		$results = $this->db->getHistorySales($id);
		if (!$results) {
			return false;
		}
		$sList = array();
		$i = 0;
		while ($row = $results->fetch_row()) {
			$s = new Order($row[0], $row[1], $row[3], $row[2], $row[4], $row[5], $row[6], $row[7]);
			$sList[$i] = $s;
			$i++;
		}
		return $sList;
	}
	
	function getMember($u) {
		$results = $this->db->getMember($u);
		$row = $results->fetch_row();
		$user = new Member($row[0], $row[1], $row[2], $row[4], $row[5], $row[6], $row[7], $row[8]);
		return $user;
	}
	
	

}

?>